package com.wisezone.permission.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.utils.BeanUtils;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.common.web.BaseController;
import com.wisezone.permission.bean.entity.WzRoleEntity;
import com.wisezone.permission.bean.search.WzRoleSearch;
import com.wisezone.permission.service.WzRescService;
import com.wisezone.permission.service.WzRoleService;

/**
 * 
 *角色; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:47 update date 2010-07-29
 *         14:44:47
 * 
 */
@WzWebAnnotation(moduleName = "role")
public class WzRoleController extends
		BaseController<WzRoleEntity, WzRoleSearch> {

	/**
	  *
	  *
	  *
	  */
	private WzRoleService wzRoleService;
	
	private WzRescService wzRescService;
	
	private String resIds;     //资源ID集合
	
	private String resNames;   //资源名称集合

	/**
	 * 返回清空条件列表
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public String toList() throws WzWebException {

		searchEntity = new WzRoleSearch();
		pagination = this.wzRoleService.listByPage(searchEntity);
		return LIST;
	}

	/**
	 * 获得列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws WzWebException {

		searchEntity = getSearchEntity();
		pagination = this.wzRoleService.listByPage(searchEntity);
		return LIST;
	}

	/**
	 * 进入新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterNew() throws WzWebException {

		if (id != null) {
			entity = this.wzRoleService.findById(id);
			entity.setId(null);
		}
		return NEW_EDIT;
	}

	/**
	 * 进入修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterEdit() throws WzWebException {

		entity = this.wzRoleService.findById(id);
		Map<String,String> map = this.wzRescService.getMapByRoleId(id);
		if(map != null){
			resIds = map.get("ids");
			resNames = map.get("names");
		}
		return NEW_EDIT;
	}

	/**
	 * 进入查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterView() throws WzWebException {

		entity = this.wzRoleService.findById(id);
		return VIEW;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws WzWebException {

		entity = getEntity();
		entity.setState(ConstantUtils.STATE_EXIST);
		this.wzRoleService.saveOrUpdate(entity,resIds);
		return toList();
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws WzWebException {

		entity = getEntity();
		WzRoleEntity oldEntity = this.wzRoleService.findById(id);
		BeanUtils.copyBean(oldEntity, entity);
		this.wzRoleService.saveOrUpdate(oldEntity,resIds);
		return toList();
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws WzWebException {

		Collection<Serializable> idList = getIdList();
		if (idList != null) {
			this.wzRoleService.delete(idList);
		}
		return toList();
	}

	public void setWzRoleService(WzRoleService wzRoleService) {
		this.wzRoleService = wzRoleService;
	}

	public String getResIds() {
		return resIds;
	}

	public void setResIds(String resIds) {
		this.resIds = resIds;
	}

	public String getResNames() {
		return resNames;
	}

	public void setResNames(String resNames) {
		this.resNames = resNames;
	}

	public void setWzRescService(WzRescService wzRescService) {
		this.wzRescService = wzRescService;
	}
	
	

}